﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using TestingIIS.Models;
using Oracle.DataAccess.Client;
using Oracle.Web;
using System.Collections;

namespace TestingIIS.Controllers
{
    public class MainSearchController : Controller
    {
        private Entities _hotelEntity = new Entities();
        //
        // GET: /MainSearch/

        public ActionResult Index()
        {
            return View();
        }

        //
        // GET: /MainSearch/Details/5

        public ActionResult Details(int id)
        {
            return View();
        }

        //
        // GET: /MainSearch/Create

        public ActionResult Create()
        {
            return View();
        } 

        //
        // POST: /MainSearch/Create

        [HttpPost]
        public ActionResult Create(FormCollection collection)
        {
            try
            {
                // TODO: Add insert logic here

                return RedirectToAction("Index");
            }
            catch
            {
                return View();
            }
        }
        
        //
        // GET: /MainSearch/Edit/5
 
        public ActionResult Edit(int id)
        {
            return View();
        }

        //
        // POST: /MainSearch/Edit/5

        [HttpPost]
        public ActionResult Edit(int id, FormCollection collection)
        {
            try
            {
                // TODO: Add update logic here
 
                return RedirectToAction("Index");
            }
            catch
            {
                return View();
            }
        }

        //
        // GET: /MainSearch/Delete/5
 
        public ActionResult Delete(int id)
        {
            return View();
        }

        //
        // POST: /MainSearch/Delete/5

        [HttpPost]
        public ActionResult Delete(int id, FormCollection collection)
        {
            try
            {
                // TODO: Add delete logic here
 
                return RedirectToAction("Index");
            }
            catch
            {
                return View();
            }
        }

        public ActionResult Search(FormCollection form, string searchString)
        {
            if (searchString == null || searchString == string.Empty)
            {
                return RedirectToAction("Index");
            }
            var radioButtonResults = form.GetValues(1);
            string resultDropList = "";
            switch(radioButtonResults[0]) //value of radiobuttonList
            {
                case "_hotel":
                    resultDropList = form.GetValues(2)[0];
                    return RedirectToAction("SearchHotel", new { dropList = resultDropList, searchString = searchString, sizeOfTable = 8});
                case "_client":
                    return RedirectToAction("SearchClient", new { dropList = resultDropList, searchString = searchString, sizeOfTable = 11});
                case "_transaction":
                    return RedirectToAction("SearchTransaction", new { dropList = resultDropList, searchString = searchString, sizeOfTable = 11});
                default:
                    return RedirectToAction("Index");
            }
        }

        public ActionResult SearchHotel(string dropList,string searchString,int sizeOfTable)
        {
            string categoryString = dropList.ToLower();
            string queryString = string.Empty;
            List<List<string>> searchResultArray = new List<List<string>>();

            switch(categoryString)
            {
                case "_name":
                    queryString = "select * from hotel h where lower(h.hotel_name) LIKE '%" + searchString.ToLower() + "%'"; 
                    break;
                case "_address":
                    queryString = "select * from hotel h where h.address LIKE '%" + searchString + "%'"; 
                    break;
                case "_country":
                    queryString = "select * from hotel h where h.country LIKE '%" + searchString + "%'"; 
                    break;
                default:
                    break;
            }
            List<List<object>> returnedList = ExecuteMyQuery(queryString, sizeOfTable);
            ViewData["val1"] = returnedList.ElementAt(0).ElementAt(0);
            ViewData["column1"] = "Name";

            //foreach (List<string> item in returnedList)
            {
           //     searchResultArray.Add(item);
            }

            return View();
        }

        public ActionResult SearchClient()
        {
            return View();
        }
        
        public ActionResult SearchTransaction()
        {
            return View();
        }

        private List<List<object>> ExecuteMyQuery(string queryString, int sizeOfTable)
        {
            OracleConnection myConnection = new OracleConnection();
            OracleCommand myCommand = myConnection.CreateCommand();
            OracleTransaction myTransaction;
            OracleDataReader myReader;
            myConnection.ConnectionString = "Data Source=sid3.comp.nus.edu.sg/sid3.comp.nus.edu.sg;User Id=A0087753;Password=cs2102std";
            myConnection.Open();
            myCommand.CommandType = System.Data.CommandType.Text;
            myCommand.CommandText = queryString;
            List<List<object>> resultArray = new List<List<object>>();

            myTransaction = myConnection.BeginTransaction(System.Data.IsolationLevel.ReadCommitted);
            myCommand.Transaction = myTransaction;
            try
            {

                if (myConnection.State == System.Data.ConnectionState.Open)
                {
                    myReader = myCommand.ExecuteReader();
                    
                    while (myReader.Read())
                    {
                        object[] result = new object[100];
                        List<object> resultTypeList = new List<object>();
                        myReader.GetValues(result);
                        resultTypeList.AddRange(result);
                        resultTypeList.RemoveRange(myReader.FieldCount, 100 - myReader.FieldCount);
                        resultArray.Add(resultTypeList);
                    }
                    myTransaction.Commit();
                    myConnection.Close();
                }
            }
            catch(Exception ex)
            {
                myTransaction.Rollback();
                if (myConnection.State == System.Data.ConnectionState.Open)
                {
                    myConnection.Close();
                }

                resultArray.Clear();
            }

            return resultArray;
        }
    }
}
